package com.etogc.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SharedPreferences工具
 * <p>
 * Created by zv on 2017-12-6.
 */
public class SpUtil {
    private Context context = null;

    private static SpUtil spInfo;

    private final String DEFAULT_SP = "YC_SP";

    private SpUtil() {
    }

    public static SpUtil getInstance() {
        if (spInfo == null) {
            spInfo = new SpUtil();
        }
        return spInfo;
    }

    public void init(Context context) {
        this.context = context;
    }

    public String getString(String key) {
        return getString(DEFAULT_SP, key, "");
    }

    public String getString(String key, String defValue) {
        return getString(DEFAULT_SP, key, defValue);
    }

    public String getString(String spName, String key, String defValue) {
        SharedPreferences sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        return sp.getString(key, defValue);
    }

    public void putString(String key, String value) {
        putString(DEFAULT_SP, key, value);
    }

    public void putString(String spName, String key, String value) {
        SharedPreferences.Editor sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE).edit();
        sp.putString(key, value);
        sp.apply();
    }

}
